<template>
  <div class="dashboard-container">
    <div class="app-container">
      <head-tool>
        <template slot="after">
          <el-button type="primary" @click="showAdd('0', 1)"
            >添加权限</el-button
          >
        </template>
      </head-tool>
      <!-- 下面的表格 -->
      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="code" label="标识"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              v-if="row.type == 1"
              type="text"
              @click="showAdd(row.id, 2)"
              >添加</el-button
            >
            <el-button type="text" @click="doEdit(row.id)">编辑</el-button>
            <el-button type="text" @click="doDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="title" :visible.sync="showDialog">
        <el-form
          ref="form"
          label-width="80px"
          :model="formData"
          :rules="formRules"
        >
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code"></el-input>
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description"></el-input>
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <el-row type="flex" justify="center">
          <el-button type="primary" size="small" @click="save">确定</el-button>
          <el-button size="small">取消</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getPermission,
  addPermission,
  delPermission,
  getPermissionDetail,
  savePermissionDetail
} from '@/api/permission'

// 导入递归函数/
import { transToTreeData } from '@/utils/index'
export default {
  data () {
    return {
      tableData: [],
      showDialog: false,
      formData: {},
      formRules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title () {
      return this.formData.id ? '编辑权限' : '添加权限'
    }
  },
  created () {
    this.getPermission()
  },
  methods: {
    async getPermission () {
      let res = await getPermission()
      // console.log(res)
      this.tableData = transToTreeData(res.data, '0')
    },
    showAdd (pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.formData={}
      this.showDialog = true
    },
    async save () {
      if (this.formData.id) {
        // 做编辑
        await savePermissionDetail(this.formData)
      } else {
        let res = await addPermission(this.formData)
      }
      // console.log(res)
      this.$message.success(this.title + '成功!')
      this.getPermission()
      this.showDialog = false
    },
    // 编辑点击事件
    async doEdit (id) {
      let res = await getPermissionDetail(id)
      //  console.log(res);
      this.formData = res.data
      this.showDialog = true
    },
   async doDel(id){
     await delPermission(id)
     this.$message.success('删除成功!')
     this.getPermission()
   }
  }
}
</script>

<style></style>
